package com.atguigu.guli.service.edu.controller.api;

import com.atguigu.guli.common.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author lj on 2021/2/6.
 * @version 1.0
 */
//@CrossOrigin
@RestController
@RequestMapping("/api/edu/teacher")
@Slf4j
public class ApiTeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;
    @ApiOperation(value="所有讲师列表")
    @GetMapping("list")
    public R listAll(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items", list).message("获取讲师列表成功");
    }

    @ApiOperation(value="所有讲师列表")
    @GetMapping("/get/{id}")
    public R selectTeacherInfoById(@PathVariable(value = "id") String id){
        //根据id获取老师信息
        final Teacher teacher = teacherService.getById(id);
        final HashMap<String, Object> teacherInfo = new HashMap<>();
        teacherInfo.put("teacher",teacher);
        //根据老师id获取老师课程信息
        final List<Course> courses = courseService.list(new QueryWrapper<Course>().eq("teacher_id", id));
        teacherInfo.put("courseInfo",courses);

        return R.ok().data("teacherInfo", teacherInfo).message("获取讲师信息成功");
    }
}
